﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTreeWidgetItem*  item=new QTreeWidgetItem();
    item->setText(0,"图片文件");
    ui->treeWidget->addTopLevelItem(item);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actAppendDIr_triggered()
{
    QString path = QFileDialog::getExistingDirectory(0,"选择文件夹",QDir::homePath());
    QDir dir(path);
    if(dir.exists()){
        QTreeWidgetItem*  item=new QTreeWidgetItem();
        item->setText(0,dir.dirName());
        item->setToolTip(0,path);
        item->setIcon(0,QIcon(":/imgs/dir.png"));
        ui->treeWidget->currentItem()->addChild(item);
    }
}


void MainWindow::on_actAppdenFile_triggered()
{
    QString path = QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"图片文件(*.jpg *.bmp *.jepg *.png);;所有文件(*.*)");
        QFile file(path);
        if(file.exists()){
            QTreeWidgetItem*  item=new QTreeWidgetItem();
            QFileInfo info(file);
            item->setText(0,info.fileName());
            item->setToolTip(0,path);
            item->setIcon(0,QIcon(":/imgs/img.png"));
            ui->treeWidget->currentItem()->addChild(item);
        }
}


void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
        QFile file(item->toolTip(0));
        if(file.exists()){
            QPixmap pix;
            pix.load(item->toolTip(0));
            if(!pix.isNull()){

                ui->label->setPixmap(pix.scaledToWidth(ui->label->width()));
            }
        }
}

